import request from '@/utils/request'


// 公共方法

export function formattedNumber(price) {
    if (!price) {
        return
    }
    var number = price.toLocaleString();
    return number
}

export function objectFun(value = '', arr = [], lvalue = 'dictValue', llabel = 'dictLabel') {
    if (!value || !arr || !arr.length) {
        return
    }
    var name = '';
    for (const item of arr) {
        if (item[lvalue] == value) {
            name = item[llabel];
        }
    }
    return name;
}

export function checkUrl(url) {
    if (!url) {
        return '';
    }
    if (url.substring(0, 4) == "http") {
        return url;
    } else {
        url = 'https://' + url;
        return url;
    }
}


export function priceSwitch(x = 0) {
    if (!x) {
        x = 0;
    }
    var f = parseFloat(x);
    if (isNaN(f)) return false;
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length < (rs + 1) + 2) {
        s += '0';
    }
    var leftNum = s.split(".")[0];
    var rightNum = "." + s.split(".")[1];
    var result;
    var resultArray = new Array();
    if (leftNum.length > 3) {
        var i = true;
        while (i) {
            resultArray.push(leftNum.slice(-3));
            leftNum = leftNum.slice(0, leftNum.length - 3);
            if (leftNum.length < 4) {
                i = false;
            }
        }
        var sortArray = new Array();
        for (var i = resultArray.length - 1; i >= 0; i--) {
            sortArray.push(resultArray[i]);
        }
        result = leftNum + "," + sortArray.join(",") + rightNum;
    } else {
        result = s;
    }
    return result;
}

export function timestampToTime(timestamp) {
    if (!timestamp) {
        return 0
    }
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + '/' + M + '/' + D + ' ' + h + ':' + m + ':' + s;
}

export function happenTimeFun(num, types = "yyyy-MM-dd hh:ii", dd = 1) {
    if (!num) {
        return '';
    }
    let zthisTime = num;
    if (dd == 1) {
        zthisTime = num + "";
        zthisTime = zthisTime.replace(/-/g, '/');

    }
    let date = new Date(zthisTime);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    if (types == 'yyyy-MM-dd hh:ii:ss') {
        return y + '/' + MM + '/' + d + ' ' + h + ':' + m + ':' + s;
    } else if (types == 'hh:ii') {
        return h + ':' + m;
    } else if (types == 'yyyy-MM-dd_text') {
        return `${y}年${MM}月${d}日`;
    } else if (types == 'yyyy-MM-dd_text_hh_mm') {
        return `${y}年${MM}月${d}日${h}:${m}`;
    } else if (types == 'yyyy/MM/dd') {
        return `${y}/${MM}/${d}`;
    } else if (types == 'yyyy-MM-dd') {
        return `${y}-${MM}-${d}`;
    } else if (types == 'MM/dd') {
        return `${MM}/${d}`;
    } else if (types == 'yyyy-MM') {
        return `${y}-${MM}`;
    } else if (types == 'MM/dd hh:ii') {
        return `${MM}/${d} ${h}:${m}`;
    } else {
        return `${y}/${MM}/${d} ${h}:${m}`;
    }
}


// 公共方法

// 营业执照识别
export function BusinessLicense(query) {
    return request({
        url: '/api/ocr/BusinessLicense',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: query
    })
}
// 身份证反面识别
export function IdCardOCRBack(query) {
    return request({
        url: '/api/ocr/IdCardOCRBack',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: query
    })
}
// 身份证正面识别
export function IdCardOCRFace(query) {
    return request({
        url: '/api/ocr/IdCardOCRFace',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: query
    })
}

// 上传文件
export function uploadByServer(query) {
    return request({
        url: '/api/oss/upload',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: query
    })
}


// 通用字典接口
export function publicList(query) {
    return request({
        url: '/system/dict/data/list',
        method: 'get',
        params: query
    })
}


// 公共地址列表
export function regionList(query) {
    return request({
        url: '/api/region/region',
        method: 'get',
        params: query
    })
}



// 完善信息


// 信息详情
export function CompanyInfo(query) {
    return request({
        url: '/user/company/info',
        method: 'get',
        params: query
    })
}

export function CompanyUpdate(query) {
    return request({
        url: '/user/company',
        method: 'put',
        data: query
    })
}

// 完善信息 END

export function getPerId() {
    return request({
        url: '/user/order/getPreId',
        method: 'get'
    })
}

export function OrderCompanyInfo(companyId) {
    return request({
        url: `/user/order/company/${companyId}`,
        method: 'get',
    })
}

export function OrderPersonInfo(personId) {
    return request({
        url: `/user/order/person/${personId}`,
        method: 'get',
    })
}

export function OrderCardInfo(cardId) {
    return request({
        url: `/user/order/card/${cardId}`,
        method: 'get',
    })
}

export function OrderHouseInfo(houseId) {
    return request({
        url: `/user/order/house/${houseId}`,
        method: 'get',
    })
}



// 订单详情
export function orderList(query) {
    return request({
        url: '/user/order/list',
        method: 'get',
        params: query
    })
}

export function relationList(query) {
    return request({
        url: '/user/order/relationList',
        method: 'get',
        params: query
    })
}

export function orderDetail(id) {
    return request({
        url: `/user/order/${id}`,
        method: 'get'
    })
}

// 获取银行列表
export function BankList(query) {
    return request({
        url: '/common/bank',
        method: 'get',
        params: query
    })
}


// 还款计划列表
export function RepaymentList(query) {
    return request({
        url: '/api/repayment/list',
        method: 'get',
        params: query
    })
}

// 订单详情 END ---------------------


// 转账

export function TransferList(query) {
    return request({
        url: '/user/transfer/userList',
        method: 'get',
        params: query
    })
}


// 放款


export function OrderPayList(query) {
    return request({
        url: '/user/order/payList',
        method: 'get',
        params: query
    })
}

export function saveCompany(data) {
  return request({
    url: '/user/order/saveBorrowerRelationCompany',
    method: 'post',
    data: data
  })
}


export function savePerson(data) {
  return request({
    url: '/user/order/saveBorrowerRelationPerson',
    method: 'post',
    data: data
  })
}

export function saveCard(data) {
  return request({
    url: '/user/order/saveCard',
    method: 'post',
    data: data
  })
}


export function saveHouse(data) {
  return request({
    url: '/user/order/saveHouse',
    method: 'post',
    data: data
  })
}


export function saveOrderPersonal(data) {
  return request({
    url: '/user/order/saveOrderPersonal',
    method: 'post',
    data: data
  })
}


export function saveOrderCompany(data) {
  return request({
    url: '/user/order/saveOrderCompany',
    method: 'post',
    data: data
  })
}


export function delCard(cardId) {
  return request({
    url: `/user/order/delCard/${cardId}`,
    method: 'post'
  })
}

export function delHouse(houseId) {
  return request({
    url: `/user/order/delHouse/${houseId}`,
    method: 'post'
  })
}


export function delPerson(personId) {
  return request({
    url: `/user/order/delPerson/${personId}`,
    method: 'post'
  })
}


export function delCompany(companyId) {
  return request({
    url: `/user/order/delCompany/${companyId}`,
    method: 'post'
  })
}


export function updateStatus(data) {
  return request({
    url: '/user/order/updateStatus',
    method: 'post',
	data: data
  })
}


export function addTransfer(data) {
  return request({
    url: '/user/order/transfer',
    method: 'post',
    data: data
  })
}

export function OrderTransferList(query) {
    return request({
        url: '/user/order/transferList',
        method: 'get',
        params: query
    })
}

export function getProductList(id) {
    return request({
        url: '/user/order/getProductList/'+id,
        method: 'get'
    })
}

export function getProductConfigList(query) {
    return request({
        url: '/user/order/getProductConfigList',
        method: 'get',
        params: query
    })
}